import { MainInfos } from './mainInfo';
let _MainInfos = MainInfos;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import MainInfoMock from './mk/mng/card/mainInfoMock';
 * MainInfoMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mk/mng/card/mainInfo/listKey').reply(config => {
      let params = config.params;
      let mockMainInfos = _MainInfos.filter(mainInfo => {
      	if(params.cardType||params.cardType==''){ if( mainInfo.cardType.indexOf(params.cardType)>=0) return true;}
      	if(params.backgroundPicUrl||params.backgroundPicUrl==''){ if( mainInfo.backgroundPicUrl.indexOf(params.backgroundPicUrl)>=0) return true;}
      	if(params.prerogative||params.prerogative==''){ if( mainInfo.prerogative.indexOf(params.prerogative)>=0) return true;}
      	if(params.autoActivate||params.autoActivate==''){ if( mainInfo.autoActivate.indexOf(params.autoActivate)>=0) return true;}
      	if(params.wxActivate||params.wxActivate==''){ if( mainInfo.wxActivate.indexOf(params.wxActivate)>=0) return true;}
      	if(params.supplyBonus||params.supplyBonus==''){ if( mainInfo.supplyBonus.indexOf(params.supplyBonus)>=0) return true;}
      	if(params.bonusUrl||params.bonusUrl==''){ if( mainInfo.bonusUrl.indexOf(params.bonusUrl)>=0) return true;}
      	if(params.supplyBalance||params.supplyBalance==''){ if( mainInfo.supplyBalance.indexOf(params.supplyBalance)>=0) return true;}
      	if(params.balanceUrl||params.balanceUrl==''){ if( mainInfo.balanceUrl.indexOf(params.balanceUrl)>=0) return true;}
      	if(params.bonusCleared||params.bonusCleared==''){ if( mainInfo.bonusCleared.indexOf(params.bonusCleared)>=0) return true;}
      	if(params.bonusRules||params.bonusRules==''){ if( mainInfo.bonusRules.indexOf(params.bonusRules)>=0) return true;}
      	if(params.balanceRules||params.balanceRules==''){ if( mainInfo.balanceRules.indexOf(params.balanceRules)>=0) return true;}
      	if(params.activateUrl||params.activateUrl==''){ if( mainInfo.activateUrl.indexOf(params.activateUrl)>=0) return true;}
      	if(params.activateAppBrandUserName||params.activateAppBrandUserName==''){ if( mainInfo.activateAppBrandUserName.indexOf(params.activateAppBrandUserName)>=0) return true;}
      	if(params.activateAppBrandPass||params.activateAppBrandPass==''){ if( mainInfo.activateAppBrandPass.indexOf(params.activateAppBrandPass)>=0) return true;}
      	if(params.discount||params.discount==''){ if( mainInfo.discount.indexOf(params.discount)>=0) return true;}
      	if(params.cardId||params.cardId==''){ if( mainInfo.cardId.indexOf(params.cardId)>=0) return true;}
      	if(params.cardPkId||params.cardPkId==''){ if( mainInfo.cardPkId.indexOf(params.cardPkId)>=0) return true;}
      	if(params.createTime||params.createTime==''){ if( mainInfo.createTime.indexOf(params.createTime)>=0) return true;}
      	if(params.leastCost||params.leastCost==''){ if( mainInfo.leastCost.indexOf(params.leastCost)>=0) return true;}
      	if(params.reduceCost||params.reduceCost==''){ if( mainInfo.reduceCost.indexOf(params.reduceCost)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockMainInfos
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mk/mng/card/mainInfo/list').reply(config => {
      let params = config.params;
      let mockMainInfos = _MainInfos.filter(mainInfo => {
      	var isTrue=false;
      	if(params.cardType||params.cardType==''){if(mainInfo.cardType.indexOf(params.cardType)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.backgroundPicUrl||params.backgroundPicUrl==''){if(mainInfo.backgroundPicUrl.indexOf(params.backgroundPicUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.prerogative||params.prerogative==''){if(mainInfo.prerogative.indexOf(params.prerogative)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.autoActivate||params.autoActivate==''){if(mainInfo.autoActivate.indexOf(params.autoActivate)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.wxActivate||params.wxActivate==''){if(mainInfo.wxActivate.indexOf(params.wxActivate)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.supplyBonus||params.supplyBonus==''){if(mainInfo.supplyBonus.indexOf(params.supplyBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.bonusUrl||params.bonusUrl==''){if(mainInfo.bonusUrl.indexOf(params.bonusUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.supplyBalance||params.supplyBalance==''){if(mainInfo.supplyBalance.indexOf(params.supplyBalance)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.balanceUrl||params.balanceUrl==''){if(mainInfo.balanceUrl.indexOf(params.balanceUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.bonusCleared||params.bonusCleared==''){if(mainInfo.bonusCleared.indexOf(params.bonusCleared)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.bonusRules||params.bonusRules==''){if(mainInfo.bonusRules.indexOf(params.bonusRules)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.balanceRules||params.balanceRules==''){if(mainInfo.balanceRules.indexOf(params.balanceRules)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.activateUrl||params.activateUrl==''){if(mainInfo.activateUrl.indexOf(params.activateUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.activateAppBrandUserName||params.activateAppBrandUserName==''){if(mainInfo.activateAppBrandUserName.indexOf(params.activateAppBrandUserName)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.activateAppBrandPass||params.activateAppBrandPass==''){if(mainInfo.activateAppBrandPass.indexOf(params.activateAppBrandPass)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.discount||params.discount==''){if(mainInfo.discount.indexOf(params.discount)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cardId||params.cardId==''){if(mainInfo.cardId.indexOf(params.cardId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cardPkId||params.cardPkId==''){if(mainInfo.cardPkId.indexOf(params.cardPkId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.createTime||params.createTime==''){if(mainInfo.createTime.indexOf(params.createTime)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.leastCost||params.leastCost==''){if(mainInfo.leastCost.indexOf(params.leastCost)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.reduceCost||params.reduceCost==''){if(mainInfo.reduceCost.indexOf(params.reduceCost)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockMainInfos
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockMainInfos.length;
      mockMainInfos = mockMainInfos.filter((mainInfo, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockMainInfos,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mk/mng/card/mainInfo/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  cardPkId  } = params;
      _MainInfos = _MainInfos.filter(mainInfo => mainInfo.cardPkId !== cardPkId);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mk/mng/card/mainInfo/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _MainInfos = _MainInfos.filter(mainInfo => !params.includes(mainInfo.cardPkId));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mk/mng/card/mainInfo/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { cardType,backgroundPicUrl,prerogative,autoActivate,wxActivate,supplyBonus,bonusUrl,supplyBalance,balanceUrl,bonusCleared,bonusRules,balanceRules,activateUrl,activateAppBrandUserName,activateAppBrandPass,discount,cardId,cardPkId,createTime,leastCost,reduceCost } = params;
      _MainInfos.some(mainInfo => {
        if (mainInfo.cardPkId === cardPkId) {
			mainInfo.cardType = cardType;
			mainInfo.backgroundPicUrl = backgroundPicUrl;
			mainInfo.prerogative = prerogative;
			mainInfo.autoActivate = autoActivate;
			mainInfo.wxActivate = wxActivate;
			mainInfo.supplyBonus = supplyBonus;
			mainInfo.bonusUrl = bonusUrl;
			mainInfo.supplyBalance = supplyBalance;
			mainInfo.balanceUrl = balanceUrl;
			mainInfo.bonusCleared = bonusCleared;
			mainInfo.bonusRules = bonusRules;
			mainInfo.balanceRules = balanceRules;
			mainInfo.activateUrl = activateUrl;
			mainInfo.activateAppBrandUserName = activateAppBrandUserName;
			mainInfo.activateAppBrandPass = activateAppBrandPass;
			mainInfo.discount = discount;
			mainInfo.cardId = cardId;
			mainInfo.cardPkId = cardPkId;
			mainInfo.createTime = createTime;
			mainInfo.leastCost = leastCost;
			mainInfo.reduceCost = reduceCost
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mk/mng/card/mainInfo/add').reply(config => {
      var params = JSON.parse( config.data );
      let { cardType,backgroundPicUrl,prerogative,autoActivate,wxActivate,supplyBonus,bonusUrl,supplyBalance,balanceUrl,bonusCleared,bonusRules,balanceRules,activateUrl,activateAppBrandUserName,activateAppBrandPass,discount,cardId,cardPkId,createTime,leastCost,reduceCost } = params;
      _MainInfos.push({
		cardType:cardType,
		backgroundPicUrl:backgroundPicUrl,
		prerogative:prerogative,
		autoActivate:autoActivate,
		wxActivate:wxActivate,
		supplyBonus:supplyBonus,
		bonusUrl:bonusUrl,
		supplyBalance:supplyBalance,
		balanceUrl:balanceUrl,
		bonusCleared:bonusCleared,
		bonusRules:bonusRules,
		balanceRules:balanceRules,
		activateUrl:activateUrl,
		activateAppBrandUserName:activateAppBrandUserName,
		activateAppBrandPass:activateAppBrandPass,
		discount:discount,
		cardId:cardId,
		cardPkId:cardPkId,
		createTime:createTime,
		leastCost:leastCost,
		reduceCost:reduceCost
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};